<template>
  <div class="login_container">
    <div class="login_box">
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerFormRules" class="register_form" label-position="right" label-width="100px">
        <el-text tag="b" style="margin-left: 110px;font-size: 18px;margin-bottom: 20px" >注册成为新用户</el-text>
        <el-form-item prop="account" label="账号" >
          <el-input v-model="registerForm.account" prefix-icon="el-icon-user-solid" placeholder="请使用账号/手机号/邮箱登录"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
        <el-input v-model="registerForm.password"  prefix-icon="el-icon-lock" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="phoneNumber" label="手机号">
        <el-input v-model="registerForm.phoneNumber"  prefix-icon="el-icon-lock"  placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱" >
          <el-input v-model="registerForm.email"  prefix-icon="el-icon-lock"  placeholder="请输入邮箱"></el-input>
        </el-form-item>

        <el-form-item class="btn">
          <el-button type="primary" @click="register">确认 </el-button>
          <el-button type="info" @click="resetregisterForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import { ElMessageBox } from 'element-plus';
export default {
  name: "UserRegister",
  data() {
    return {
      registerForm: {
        account: '',
        password: '',
        email:'',
        phoneNumber:'',
      },
      registerFormRules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        phoneNumber: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
      }
    }
  },


  methods:{
    register() {
      this.$refs.registerFormRef.validate((valid) => {
        if (valid) {
          // 发送 POST 请求
          axios.post('http://39.101.75.128:8080/user/add', this.registerForm)
              .then(response => {
                if (response.data.code == 200) {
                  ElMessageBox.alert('注册成功', '提示', {
                    confirmButtonText: '确定',
                    callback: () => {
                      // 跳转回登录页面
                      this.$router.push('/login');
                    }
                  });
                } else {
                  ElMessageBox.alert('注册失败，请重试', '提示', {
                    confirmButtonText: '确定'
                  });
                }
              })
              .catch(error => {
                console.error(error);
                ElMessageBox.alert('网络错误，请稍后再试', '提示', {
                  confirmButtonText: '确定'
                });
              });
        } else {
          // 表单验证不通过时的处理
          return false;
        }
      });
    },
      resetregisterForm() {
        // 重置注册表单
        this.$refs.registerFormRef.resetFields();
      }
  }

}
</script>

<style scoped>
.login_container {
  box-sizing: unset !important;
  height: 100%;
  /*background-color: #2b4b6b;*/
  background: url('@/assets/img/bg_ljj.jpg');
}
.el-input{
  width: 80%;
  height: 30px;
  border: 1px #475669;
}

.login_box {
  width: 500px;
  height: 350px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.el-form-item{
  margin-top: 15px;
}

.login_box .avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}

.login_box .avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.register_form {
  margin-left: 30px;
  margin-top: 30px;
  position: absolute;
  bottom: 0;
  width: 90%;
  height: 90%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btn {
  margin-left: 30px;
  display: flex;
  justify-content: flex-end;
}
</style>